package session;

import utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 获取Session(tomcat完成真实创建)
 */
public class S1_CreateSession extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("S1_CreateSession 被调用...");

        //1.获取session，同时也可能创建session
        HttpSession session = req.getSession();
        //2.给session获取id
        System.out.println("当前sessionId=" + session.getId());
        //3.给session存放数据
        session.setAttribute("email", "zs@qq.com");

        //4.回送浏览器
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print("<h1>创建/操作session成功~</h1>");
        writer.flush();
        writer.close();
    }
}
